var jqueryScript = document.createElement('script');
jqueryScript.src = '/Scripts/jquery-1.5.1.min.js';
jqueryScript.type = 'text/javascript';
var head = document.getElementsByTagName('head')[0];
head.appendChild(jqueryScript);

CKFinder.dialog.add("ImageEditingDialog", function (api) {
    return {
        title: "Edit",
        minWidth: 500,
        minHeight: 520,
        onShow: function () {
            var doc = this.getElement().getDocument();
            var frame = doc.getById('frame');
            var selectedUrl = api.getSelectedFile().getUrl();
            var url = 'http://localhost:82/Home/EditImage?imageUrl=' + encodeURIComponent(window.location.host + selectedUrl)
                + '&savePath=' + encodeURIComponent('D:\\Projects\\CroppingImageInvestigation\\CroppingImageInvestigation\\ckfinder\\userfiles\\images');
            frame.setAttribute('src', url);
        },
        contents: [
                {
                    id: 'tab1',
                    label: '',
                    title: '',
                    expand: true,
                    padding: 0,
                    elements: [
                            {
                                type: 'html',
                                html: '<iframe id="frame" frameBorder="0" src="" style="width:500px; height:510px;" />'
                            }
                    ]
                }
            ],
        onOk: function () {
            refresh(this, api);
        },
        onCancel: function () {
            refresh(this, api);
        },
        buttons: [CKFinder.dialog.okButton]
    };
});

CKFinder.addPlugin('ImageCropper', function (api) {

    api.addFileContextMenuOption({ label: 'Edit', command: "ImageCropper" }, function (api, file) {
        api.openDialog("ImageEditingDialog");
        if (!file.isImage()) {
            api.openMsgDialog("Image cropping", "This feature is only available for editing images.");
            return;
        } //end check if isImage()
    });

});

function refresh(ckfinder, api) {
    var doc = ckfinder.getElement().getDocument();
    var frame = doc.getById('frame');
    frame.setAttribute('src', '');
    api.refreshOpenedFolder();
}
